package org.eproject.core.well.command;

import java.util.Date;
import java.util.Set;

import org.eproject.core.hibernate.pojo.SStation;
import org.eproject.core.hibernate.pojo.SUsers;
import org.eproject.core.well.delegate.UserDeleGate;
import org.eproject.core.well.exception.CommandException;
import org.eproject.core.well.exception.DeleGateException;
import org.eproject.core.well.exception.FactoryException;
import org.eproject.core.well.factory.Simplefactory;
import org.eproject.core.well.viewhelp.Helper;

public class AddUserCommand implements ICommand{

	public String execute(Helper h) throws CommandException {
		String uid = (String)h.getData().get("uid");
		String uname = (String)h.getData().get("uname");
		if(uname == null) return "/eproject/main/sysadmin/adduser.jsp";
		String upasswd = (String)h.getData().get("upasswd");
		String email = (String)h.getData().get("email");
		String ubirth = (String)h.getData().get("ubirth");
		String uphone = (String)h.getData().get("uphone");
		String umark = (String)h.getData().get("umark");
		SUsers u = new SUsers();
		u.setUemail(email);
		u.setUname(uname);
		u.setUpasswd(upasswd);
		Set sset = u.getSStations();
		SStation ss = new SStation();
		sset.add(ss);
		u.setUmark(umark);
		u.setUbirth(new Date());
		try {
			UserDeleGate ud = (UserDeleGate)Simplefactory.creatorBean("userdelegate");
			ud.addUser(u);
			return "/eproject/main/sysadmin/adduser.jsp";
		} catch (FactoryException e) {
			e.printStackTrace();
			throw new CommandException(e);
		} catch (DeleGateException e) {
			e.printStackTrace();
			throw new CommandException(e);
		}
	}

}
